<?php
/**
 * 将积分转到新表
 *
 * 命令：
 * bash$ ./cli score
 * $Id: score.php 4 2011-12-15 09:28:04Z 周永隆 $
 */
class CLI_Score extends CLI
{

    /**
     * 脚本主方法
     *
     * @return void
     */
    public function main()
    {
        // 记录开始时间
        $time_start = microtime(TRUE);

        $offset=0;
        $per_count=100;
        $statics=TRUE;

        // 循环获取所有记录
        while (true)
        {
            $rows = DB::select('event_id','member_id','amount_change','event_time','description')
                ->from('vcurrency_event')
                ->order_by('event_id', 'ASC')
                ->limit($per_count)
                ->offset($offset)
                ->fetch_all();

            // 记录集遍历完毕
            if(count($rows)<1){
                // 计算脚本运行耗时
                $time_script = round( microtime(TRUE) - $time_start , 3);
                CLI::output("Done at:" . date('Y-m-d H:i:s') ." \nscript time:" . $time_script);
                die();
            }

            // 根据记录集进行计算
            $j=0;
            foreach($rows as $item){
                $j++;
                if(true){
                    if (trim($item['description'])=='登录')
                    {
                        $type ='0';
                        $plugin_name='login';
                    }
                    else
                    {
                        $type ='1';
                        $plugin_name='pay';
                    }

                    // 导出记录到新表
                    DB::insert('member_score',array(
                            'member_id',
                            'score_type',
                            'time',
                            'score',
                            'plugin_name',
                        ))
                        ->values(array(
                            $item['member_id'],
                            $type,
                            $item['event_time'],
                            $item['amount_change'],
                            $plugin_name,
                        ))
                        ->execute();

                    // 汇总积分
                    if ($statics)
                    {
                        try
                            {
                                var_dump(mysql_query("INSERT INTO `member_score_total` (`member_id`,`score`,`score_buy`) VALUES ('".$item['member_id']."','0','0');"));
                            }
                            catch (Exception $e)
                            {
                                echo 'err:'.$e->getMessage(), "\n";
                            }

                            var_dump(mysql_query("UPDATE `member_score_total` SET
`score`=(SELECT SUM(`score`) FROM `member_score` WHERE `score_type`='0' AND `member_id`='".$item['member_id']."'),
`score_buy`=(SELECT SUM(`score`) FROM `member_score` WHERE `score_type`='1' AND `member_id`='".$item['member_id']."')
WHERE `member_id`='".$item['member_id']."';"));
                    }

                    CLI::output  ($offset + $j . ",event_id:" .  $item['event_id'] . " Done \n");
                }



            }

            unset($rows);

            $offset=$offset+$per_count;
        }
    }



}